from django.db import connection
from django.http import JsonResponse
from django.shortcuts import render


def report(request):
    context = {}
    return render(request, 'report.html', context)


def get_province_data(request):
    """
    省份疫情数据接口
    """
    sql = """
        select province_name, total_confirm
        from province_data
        where record_date = (
            select max(record_date)
            from province_data
            )
    """
    cursor = connection.cursor()
    cursor.execute(sql)
    data_list = []
    for item in cursor.fetchall():
        data = {'name': item[0], 'value': item[1]}
        data_list.append(data)
    return JsonResponse({'data_list': data_list})


def get_add_data(request):
    """
    每日新增数据接口
    """
    sql = """
        select record_date, add_confirm, add_suspect, add_dead, add_heal
        from total_data
        order by record_date
    """
    cursor = connection.cursor()
    cursor.execute(sql)
    day_list = []
    add_confirm_list = []
    add_suspect_list = []
    add_dead_list = []
    add_heal_list = []

    for item in cursor.fetchall():
        day_list.append(str(item[0])[5:])
        add_confirm_list.append(item[1])
        add_suspect_list.append(item[2])
        add_dead_list.append(item[3])
        add_heal_list.append(item[4])

    return JsonResponse(
        {'day_list': day_list,
         'add_confirm_list': add_confirm_list,
         'add_suspect_list': add_suspect_list,
         'add_dead_list': add_dead_list,
         'add_heal_list': add_heal_list},
    )
